package com.shanling.shanlingcontroller.ui.fragment;

import android.os.Bundle;
import android.support.v4.media.MediaMetadataCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseLazyFragment;
import com.shanling.shanlingcontroller.bean.BluetoothState;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.Music;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.music.event.MetaChangedEvent;
import com.shanling.shanlingcontroller.music.player.PlayManager;
import com.shanling.shanlingcontroller.music.player.SongLoader;
import com.shanling.shanlingcontroller.ui.activity.EQActivity;
import com.shanling.shanlingcontroller.ui.activity.MainActivity;
import com.shanling.shanlingcontroller.utils.CoverLoader;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.utils.UIUtils;

import org.litepal.LitePal;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ch1 on 2019/11/14.
 */

public class MusicCoverFragment extends BaseLazyFragment {
    @BindView(R.id.iv_cover)
    ImageView ivCover;
    @BindView(R.id.iv_love)
    ImageView ivLove;
    @BindView(R.id.iv_eq)
    ImageView ivEq;
    @Override
    protected void onLazyLoad() {
        Bundle arguments = getArguments();
        Music music =arguments.getParcelable("music");
        setCover(music);
        setLove(music);
    }
    private void setLove(Music music) {
        if(music==null){
            return;
        }
        if(!music.isLove()){
            ivLove.setImageResource(R.drawable.player_icon_collect_default);
        }else {
            ivLove.setImageResource(R.drawable.player_icon_collect_selected);
        }
    }

    private void setCover(Music music){
        if(music==null){
            Glide.with(getActivity()).load(R.drawable.player_img_default).into(ivCover);
        }else {
            Glide.with(getActivity()).load(music.getCoverUri()).error(R.drawable.player_img_default).into(ivCover);
        }
    }
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_musiccover;
    }

    @Override
    protected void initViewsAndEvents() {

    }
    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }
    @Override
    protected void onEventComing(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
           case  Constants.METACHANGE:
               if(!isOnRusme){
                   return;
               }
               MetaChangedEvent eventCenterData = (MetaChangedEvent) eventCenter.getData();
               setCover(eventCenterData.getMusic());
               setLove(eventCenterData.getMusic());
                break;
        }
    }
    @OnClick({R.id.iv_love, R.id.iv_eq})
    public void onViewClicked(View view) {
        Music playingMusic = PlayManager.getPlayingMusic();
        if (playingMusic == null) {
            ToastUtils.showToast(getActivity(),R.string.nomusic_toplay);
            return;
        }
        switch (view.getId()) {
            case R.id.iv_love:
                UIUtils.INSTANCE.collectMusic(ivLove, playingMusic);
                break;
            case R.id.iv_eq:
                if(CustomApplication.getInstance().isConnected()){
                    readyGo(EQActivity.class);
                }else {
                    ToastUtils.showToast(getActivity(),R.string.Please_connectwithBluetooth);
                }
                break;
        }
    }
}
